package me.hgj.jetpackmvvm.base.fragment

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import me.hgj.jetpackmvvm.R
import me.hgj.jetpackmvvm.ext.util.screenHeight


abstract class BaseBottomSheetDialogFragment<DB : ViewDataBinding> : BottomSheetDialogFragment() {
    lateinit var mDataBinding: DB

    /**
     * 顶部向下偏移量
     */
    var topOffset = 0
    var behavior: BottomSheetBehavior<FrameLayout>? = null
        private set

    abstract fun getLayoutResource(): Int

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return if (context == null) {
            super.onCreateDialog(savedInstanceState)
        } else BottomSheetDialog(requireContext(), R.style.TransparentBottomSheetStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mDataBinding = DataBindingUtil.inflate(inflater, getLayoutResource(), container, false)
        mDataBinding.lifecycleOwner = this
        return mDataBinding.root
    }

    override fun onStart() {
        super.onStart()
        // 设置软键盘不自动弹出
        dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        val dialog = dialog as BottomSheetDialog?
        val bottomSheet =
            dialog!!.delegate.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
        if (bottomSheet != null) {
            val layoutParams = bottomSheet.layoutParams as CoordinatorLayout.LayoutParams
            if (topOffset > 0) {
                layoutParams.height = requireContext().screenHeight - topOffset
            } else {
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
            }
            behavior = BottomSheetBehavior.from(bottomSheet)
            // 初始为展开状态
            behavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            behavior?.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                override fun onStateChanged(bottomSheet: View, newState: Int) {
                    hideKeyBoard()
                }

                override fun onSlide(bottomSheet: View, slideOffset: Float) {
                }
            })
        }
    }// 使用Point已经减去了状态栏高度


    open fun hideKeyBoard() {
        var view: View? = null
        var imm: InputMethodManager? = null
        imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (dialog?.window != null) {
            view = dialog?.window?.currentFocus
        }
        if (null != view) {
            imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }

}